<template>
    <el-card class="box-card">
        <template #header>
            <div class="card-header">
                <span>摄影后台管理系统</span>
            </div>
        </template>
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
            class="demo-ruleForm">

            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username" @keyup.enter="submitForm(ruleFormRef)" />
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input v-model="ruleForm.pwd" type="password" autocomplete="off" @keyup.enter="submitForm(ruleFormRef)"/>
            </el-form-item>
           
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router';
import { login } from '../services/user';
const ruleFormRef = ref();
const router = useRouter();

const ruleForm = reactive({
    username: '',
    pwd: ''
})

const rules = reactive({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { pattern: /^\w{5,30}$/, message:'用户名不能少于6位', trigger: 'blur' }
    ],
    pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { pattern: /^.{6,50}$/, message: '密码不能少于6位', trigger: 'blur' }
    ]
})

const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            let res = await login(ruleForm);
            if(res.code == 1){
                localStorage.setItem("access_token",res.data.access_token);
                localStorage.setItem("refresh_token",res.data.refresh_token);
                localStorage.setItem("expire_time",res.data.expire_time);
                router.push("/manage");
            }else{

            }
           
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style>
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

/* .box-card {
    width: 480px;
    margin: auto;
} */
</style>